package jp.co.systemi.asp.workflow;

import java.util.logging.Logger;

import jp.co.systemi.asp.workflow.apply.ApplyRef;
import jp.co.systemi.asp.workflow.approve.ApproveRef;
import jp.co.systemi.asp.workflow.report.PaidHolidayReport;
import jp.co.systemi.asp.workflow.route.RouteReg;
import jp.co.systemi.asp.workflow.user.UserRef;
import jp.co.systemi.asp.workflow.webservice.WebServicePage;

import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.authentication.AuthenticatedWebApplication;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.HttpSessionStore;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.session.ISessionStore;
import org.apache.wicket.settings.IExceptionSettings;
import org.apache.wicket.util.lang.PackageName;

public class WorkflowApplication extends AuthenticatedWebApplication {
    @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(WorkflowApplication.class
            .getName());
    /**
     * Constructor
     */
    public WorkflowApplication()
    {
    }

    @Override
    public Class<? extends Page> getHomePage() {
        return UserRef.class;
    }

    @Override
    public WorkflowSession newSession(Request request, Response response) {
        return new WorkflowSession(request);
    }

    @Override
    protected ISessionStore newSessionStore() {
        return new HttpSessionStore(this);
    }

    @Override
    protected void init() {
        super.init();

        getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
        getRequestCycleSettings().setResponseRequestEncoding("UTF-8");

        /* 各種Error発生時の表示 */
        getApplicationSettings().setPageExpiredErrorPage(Expire.class);
        getApplicationSettings().setAccessDeniedPage(ErrorPage.class);
        getApplicationSettings().setInternalErrorPage(ErrorPage.class);
        getExceptionSettings().setUnexpectedExceptionDisplay(IExceptionSettings.SHOW_INTERNAL_ERROR_PAGE);

        /* 各ページのBookmark化 */
        mount("/apply", PackageName.forPackage(ApplyRef.class.getPackage()));
        mount("/approve", PackageName.forPackage(ApproveRef.class.getPackage()));
        mount("/report", PackageName.forPackage(PaidHolidayReport.class.getPackage()));
        mount("/route", PackageName.forPackage(RouteReg.class.getPackage()));
        mount("/webservice", PackageName.forPackage(WebServicePage.class.getPackage()));

    }

    @Override
    public RequestCycle newRequestCycle(Request request, Response response) {
        return new WorkflowRequestCycle(this, (WebRequest)request, (WebResponse)response);
    }

    @Override
    protected Class<? extends WebPage> getSignInPageClass() {
        return SignInPage.class;
    }

    @Override
    protected Class<? extends AuthenticatedWebSession> getWebSessionClass() {
        return WorkflowSession.class;
    }

    @Override
    protected void onUnauthorizedPage(Page page) {
        throw new RestartResponseAtInterceptPageException(UnAuthorizedPage.class);
    }
}
